package com.jetwinner.util;

import java.util.Random;
import java.util.UUID;

/**
 * @author xulixin
 */
public final class UUIDUtil {

    private UUIDUtil() {
        // reserved.
    }

    public static String uniqid() {
        return UUID.randomUUID().toString();
    }

    public static String uniqid(String prefix, boolean hasEntropy) {
        long timestamp = System.currentTimeMillis();
        String entropy = "";
        if (hasEntropy) {
            generateRandomString(16); // 生成16位随机字符串
        }
        return prefix + timestamp + entropy;
    }

    public static String generateRandomString(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(characters.charAt(random.nextInt(characters.length())));
        }
        return sb.toString();
    }
}
